<template>
  <d2-container>
    <template slot="header">
      <el-form
        :inline="true"
        :model="queryForm"
        ref="queryForm"
        size="mini"
        style="margin-bottom: -18px;"
      >
        <el-form-item label="所属区域" prop="areaId">
          <el-cascader
            :props="props"
            :options="treeData"
            :show-all-levels="false"
            expand-trigger="hover"
            style="width: 100px;"
            v-model="queryForm.areaId"
            change-on-select
          ></el-cascader>
        </el-form-item>

        <el-form-item label="关键字" prop="type">
          <el-select v-model="queryForm.type" placeholder="请选择关键字" style="width: 100px;">
            <el-option label="全部" value="1" />
            <el-option label="客户编号" value="2" />
            <el-option label="客户名称" value="3" />
            <el-option label="手机号码" value="4" />
          </el-select>
        </el-form-item>

        <el-form-item label prop="searchVal">
          <el-input v-model="queryForm.searchVal" placeholder="请输入" style="width: 200px;" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleFormSubmit">
            <d2-icon name="search" />查询
          </el-button>
        </el-form-item>
      </el-form>
    </template>
    <split-pane :min-percent="10" :max-percent="50" :default-percent="50" split="vertical">
      <template slot="paneL">
        <d2-crud
          ref="d2Crud"
          class="cursorTable"
          :columns="columns"
          :data="data"
          :loading="loading"
          :pagination="pagination"
          @pagination-current-change="paginationCurrentChange"
          @current-change="handleCurrentChange"
        ></d2-crud>
      </template>
      <template slot="paneR">
        <d2-crud
          ref="d2Crud"
          :columns="columnsRight"
          :data="dataRight"
          :rowHandle="rowHandle"
          @customCz="handleRowCz"
          :loading="loadingRiht"
        ></d2-crud>
      </template>
    </split-pane>
    <el-dialog :visible.sync="rechargeBill" v-if="rechargeBill" title="充值窗口">
      <div class="demo-input-suffix" style="width: 50%;display: inline-block;margin-bottom: 10px;">
        户号：
        <el-input
          placeholder="请输入内容"
          :disabled="true"
          v-model="selectedRow.householdSerialnumber"
          style="width: 200px"
        ></el-input>
      </div>
      <div class="demo-input-suffix" style="width: 50%;display: inline-block;margin-bottom: 10px;">
        表计地址：
        <el-input
          v-model="selectedRow.serialnumber"
          placeholder="请输入内容"
          :disabled="true"
          style="width: 200px"
        ></el-input>
      </div>
      <div class="demo-input-suffix" style="width: 50%;display: inline-block;margin-bottom: 10px;">
        地址：
        <el-input
          placeholder="请输入内容"
          v-model="selectedRow.householdAddress"
          :disabled="true"
          style="width: 200px"
        ></el-input>
      </div>
      <div class="demo-input-suffix" style="width: 50%;display: inline-block;margin-bottom: 10px;">
        充值单号：
        <el-input
          placeholder="请输入内容"
          v-model="recharge.rechargeNumber"
          :disabled="true"
          style="width: 200px"
        ></el-input>
      </div>
      <div class="demo-input-suffix" style="width: 50%;display: inline-block;margin-bottom: 10px;">
        日期：
        <el-input placeholder="请输入内容" :disabled="true" v-model="time" style="width: 200px"></el-input>
      </div>
      <div class="demo-input-suffix" style="width: 50%;display: inline-block;margin-bottom: 10px;">
        充值金额：
        <el-input placeholder="请输入内容" v-model="recharge.rechargeMoney" style="width: 200px"></el-input>
      </div>
      <div class="demo-input-suffix" style="width: 50%;display: inline-block;margin-bottom: 10px;">
        支付方式：
        <el-select v-model="payKey" placeholder="请选择" style="width: 220px;">
          <el-option
            v-for="(item, index) in recharge.payChannelList"
            :key="index"
            :label="item.value"
            :value="item.key"
          />
        </el-select>
      </div>
      <div class="btn-box" style="width: 92%; text-align: right; padding-right: 20px">
        <el-button @click="rechargeSure" type="primary">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="rechargeListShow" v-if="rechargeListShow" title="充值记录">
      <d2-crud
        ref="d2Crud"
        :columns="rechargeColumns"
        :data="rechargeData"
        :loading="loading"
        :pagination="paginationRecharge"
        @pagination-current-change="paginationRechargeCurrentChange"
      ></d2-crud>
    </el-dialog>
  </d2-container>
</template>

<script>
import util from "@/libs/util";

export default {
  name: "customer",
  data() {
    return {
      householdId: null,
      rechargeListShow: false, // 充值记录弹框
      rechargeBill: false, // 退费弹框
      selectedRow: null,
      recharge: null, // 退费信息
      payKey: 2, // 支付方式
      time: null,
      labelPosition: "right",
      loading: false,
      loadingRiht: false,
      pagination: {
        currentPage: 1,
        pageSize: 100,
        total: 0
      },
      paginationRecharge: {
        currentPage: 1,
        pageSize: 100,
        total: 0
      },
      queryForm: {
        areaId: ["1"],
        type: "1",
        searchVal: ""
      },
      searchForm: {
        id: "",
        areaName: "",
        customerFee: "",
        customerName: "",
        customerPhone: "",
        customerTel: "",
        receNumber: "",
        fee: 0,
        balance: 0
      },
      rechargeData: [],
      rechargeColumns: [
        {
          title: "序号",
          key: "serialNum",
          width: 50
        },
        {
          title: "充值编号",
          key: "rechargeNumber",
          sortable: true
        },
        {
          title: "充值渠道",
          key: "rechargeChannelName",
          sortable: true
        },
        {
          title: "充值金额",
          key: "rechargeMoney",
          sortable: true
        },
        {
          title: "充值时间",
          key: "createDate",
          sortable: true
        }
      ],
      columns: [
        {
          title: "序号",
          key: "serialNum",
          width: 50
        },
        {
          title: "客户编号",
          key: "customerSerialnumber",
          sortable: true
        },
        {
          title: "客户名称",
          key: "customerName",
          sortable: true
        },
        {
          title: "手机号码",
          key: "customerPhone",
          sortable: true
        },
        {
          title: "客户地址",
          key: "areaName",
          sortable: true
        }
      ],
      columnsRight: [
        {
          title: "户号",
          key: "householdSerialnumber",
          sortable: true,
          component: {
            render: (h, row, node, index) => {
              return (
                <span
                  onclick={() => {
                    this.clickState(row);
                  }}
                  style="cursor:pointer;color:#409EFF"
                >
                  {row.householdSerialnumber}
                </span>
              );
            }
          }
        },
        {
          title: "地址",
          key: "householdAddress",
          sortable: true
        },
        {
          title: "未结算金额",
          key: "unPayFee",
          sortable: true
        }
      ],
      data: [],
      dataRight: [],
      treeData: [],
      props: {
        children: "children",
        label: "name",
        disabled: "disabled",
        value: "id"
      },
      values: [],
      selectParams: {
        size: "mini"
      },
      rowHandle: {
        columnHeader: "操作",
        custom: [
          {
            text: "充值",
            show: true,
            type: "primary",
            // disabled: function (index, row) {
            //   if (row.householdStatus === 3) {
            //     return true
            //   } else {
            //     return false
            //   }
            // },
            size: "mini",
            emit: "customCz",
            float: "right"
          }
        ]
      },
      treeParams: {
        "default-expand-all": true,
        data: [],
        props: {
          children: "children",
          label: "name",
          disabled: "disabled",
          value: "id"
        }
      }
    };
  },
  mounted() {
    this.fetchData();
    this.initTree();
  },
  methods: {
    // 点击户号 充值记录
    clickState(row) {
      this.householdId = row.id;
      this.rechargeListShow = true;
      this.getRechargePage();
    },
    // 获取充值记录
    getRechargePage() {
      this.$api
        .admin_customerFee_getRechargePage({
          householdId: this.householdId
        })
        .then(res => {
          this.rechargeData = res.data;
          this.paginationRecharge.total = res.total;
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 点击充值
    handleRowCz(row) {
      this.selectedRow = row.row;
      this.getRechargeBill(row.row.id);
    },
    // 获取充值单号
    getRechargeBill(id) {
      this.$api
        .admin_customerFee_getRechargeParam({
          householdId: id
        })
        .then(res => {
          this.time = `${new Date().getFullYear()}-${new Date().getMonth()}-${new Date().getDate()} ${new Date().getHours()}:${new Date().getMinutes()}:${new Date().getSeconds()}`;
          this.recharge = res;
          this.rechargeBill = true;
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {
          this.loading = false;
        });
    },

    // 确认充值
    rechargeSure() {
      if (!this.recharge.rechargeMoney) {
        this.$message.error("请输入充值金额");
        return;
      }
      this.$api
        .admin_customerFee_doRecharge({
          householdId: this.selectedRow.id,
          rechargeChannel: this.payKey,
          rechargeMoney: this.recharge.rechargeMoney,
          rechargeNumber: this.recharge.rechargeNumber
        })
        .then(res => {
          this.rechargeBill = false;
          this.$message.success("操作成功");
          this.getCustomerFeeData(this.selectedRow.archiveId);
          console.log(res);
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {
          this.loading = false;
        });
    },

    // 保存缴费
    handleSaveData() {
      this.$api
        .admin_customerFee_refund({
          fee: this.searchForm.fee,
          id: this.searchForm.id,
          receNumber: this.searchForm.receNumber
        })
        .then(res => {
          this.$message({
            message: "操作成功",
            type: "success"
          });
          this.getCustomerFeeData(this.searchForm.id);
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {});
    },
    // 修改余额
    changeBalance(value) {
      this.searchForm.balance =
        this.searchForm.customerFee + this.searchForm.fee;
    },
    // 获取户号列表
    getCustomerFeeData(rowId) {
      this.loadingRiht = true;
      this.$api
        .admin_customerFee_getHouseHoldList({
          areaArchiveId: rowId
        })
        .then(res => {
          console.log(res);
          this.loadingRiht = false;
          this.dataRight = res;
        })
        .catch(err => {
          this.loadingRiht = false;
          console.log("err", err);
        })
        .finally(() => {});
    },
    // 单选row
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.getCustomerFeeData(currentRow.id);
    },
    // 获取树
    initTree() {
      this.$api
        .admin_area_getList()
        .then(res => {
          this.treeData = util.getTree(res);
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {});
    },
    // 充值列表分页操作
    paginationRechargeCurrentChange(currentPage) {
      this.paginationRecharge.currentPage = currentPage;
      this.getRechargePage();
    },
    // 分页操作
    paginationCurrentChange(currentPage) {
      this.pagination.currentPage = currentPage;
      this.fetchData();
    },
    onAreaChange(data, node) {
      this.queryForm.areaId = data.id;
      this.queryForm.areaName = data.name;
      this.fetchData();
    },
    fetchData() {
      this.loading = true;
      this.$api
        .admin_customerFee_getPageList({
          pageNum: this.pagination.currentPage,
          pageSize: this.pagination.pageSize,
          areaId: this.queryForm.areaId.pop(),
          customerSerialnumber:
            this.queryForm.type == 2 ? this.queryForm.searchVal : "",
          customerName:
            this.queryForm.type == 3 ? this.queryForm.searchVal : "",
          customerPhone:
            this.queryForm.type == 4 ? this.queryForm.searchVal : ""
        })
        .then(res => {
          this.data = res.data;
          this.pagination.total = res.total;
          if (res.data.length > 0) {
            this.getCustomerFeeData(res.data[0].id);
          }
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleFormSubmit() {
      this.pagination.currentPage = 1;
      this.fetchData();
    }
  }
};
</script>
<style lang="scss" scoped >
.btnBox {
  display: flex;
  justify-content: center;
}
</style>
<style>
.cursorTable tr {
  cursor: pointer;
}
</style>
